home *** CD-ROM | disk | FTP | other *** search
/ Alles Voor Internet / Tout Pour Internet / alles voor internet.iso / MacInternet™ / Telnet / NCSA / tn3270 2.4d7 source / NCSA⁄BYU TCP⁄IP / binsubs.c < prev    next >
Text File  |  1991-08-08  |  9KB  |  456 lines

  1. #ifndef lint
  2. static char *SCCSid = "%W%    (NCSA)    %G%";
  3. #endif
  4. /*
  5. *    binsubs.c
  6. *     by Gaige B. Paulsen
  7. ****************************************************************************
  8. *                                                                          *
  9. *      Uses    :                                                               *
  10. *      TCP/IP kernel for NCSA Telnet                                       *
  11. *      by Tim Krauskopf                                                    *
  12. *       with Macintosh code by Gaige B. Paulsen                                 *
  13. *                                                                          *
  14. *      National Center for Supercomputing Applications                     *
  15. *      152 Computing Applications Building                                 *
  16. *      605 E. Springfield Ave.                                             *
  17. *      Champaign, IL  61820                                                *
  18. *                                                                          *
  19. *                                                                          *
  20. ****************************************************************************
  21. *
  22. *    MacBinary Subroutines.    
  23. *
  24. *    Called by:
  25. *        bkgr.c
  26. */
  27.  
  28. #include <Windows.h>
  29. #include <Dialogs.h>
  30. #include <Files.h>
  31. #include <Packages.h>
  32. #include <Memory.h>
  33.  
  34. #include <stdio.h>
  35. #include <fcntl.h>
  36. #include "mpw.h"
  37. #include <string.h>
  38. #include <strings.h>
  39. #include "configrec.h"
  40. #include "maclook.h"
  41. #include "bkgr.h"
  42. #include "MacBinary.h"
  43.  
  44. extern MBFile        /* BYU */
  45.     *ftp_mbfp,        /* BYU */
  46.     *mbfp;            /* BYU */
  47.  
  48. char *strncpy();
  49.  
  50. #define BLOCKS(x)    ((x+127)/128)
  51. #define lmove(f,t)    movmem(f,t,4)
  52.  
  53. #define MyFavouriteTextEditor '????'
  54.  
  55. MBHead
  56.     *mbh;
  57. char 
  58.     buffer[128];
  59.  
  60. char *strsave( p)
  61. char *p;
  62. {
  63.     char *t;
  64.  
  65.     t=NewPtr(strlen(p));
  66.     return strncpy(t,p,strlen(p));
  67. }
  68.  
  69. GetFileInfo(vol,name,iop)
  70. short vol;
  71. char *name;
  72. FileParam *iop;
  73. {
  74.     iop->ioNamePtr = name;
  75.     iop->ioVRefNum=vol;
  76.     iop->ioFVersNum=iop->ioFDirIndex=0;
  77.     PBGetFInfo((ParmBlkPtr) iop, FALSE);
  78. }
  79.  
  80. SetFileInfo(vol,name,iop)
  81. short vol;
  82. char *name;
  83. FileParam *iop;
  84. {
  85.     iop->ioNamePtr = name;
  86.     iop->ioVRefNum=vol;
  87.     iop->ioFVersNum=iop->ioFDirIndex=0;
  88.     PBSetFInfo((ParmBlkPtr) iop, FALSE);
  89. }
  90.  
  91. MakeTextFile(vol,name,iop)
  92. short vol;
  93. char *name;
  94. FileParam *iop;
  95. {    GetFileInfo(vol,name,iop);
  96.     iop->ioFlFndrInfo.fdType = 'TEXT';
  97.     iop->ioFlFndrInfo.fdCreator = MyFavouriteTextEditor;
  98.     SetFileInfo(vol,name,iop);
  99. }
  100.  
  101. isMacBinary(p)
  102. MBHead *p;
  103. {
  104.     return( (p->nlen > 0)   &&
  105.             (p->nlen < 65)  &&
  106.             (p->zero1 == 0) &&
  107.             (p->zero2 == 0) &&
  108.             (p->zero3 == 0));
  109. }
  110.  
  111. int MBsize
  112.   (
  113.     MBFile *mbfp
  114.   )
  115. {
  116.     long int size;
  117.     int ret;
  118.     
  119.     size = 0;
  120.     
  121.     ret = GetEOF( mbfp->fd, &size );            /* length of file data fork */
  122.     if (ret != noErr) 
  123.         size = 0;
  124.  
  125.     return(size);
  126. }
  127.  
  128. MBFile *MBopen( file, vrefnum, mode)
  129. char *file;
  130. short vrefnum,mode;
  131. {
  132.     MBFile *mbfp;
  133.     int err;
  134.  
  135.     if (strlen(file) > 63)             /* BYU */
  136.         return(0L);                    /* BYU */
  137.  
  138.     if (mode & MB_WRITE)
  139.         putln("MBOpen for write");
  140.     else 
  141.         putln("MBOpen for read");
  142.  
  143.     if (mode & MB_DISABLE)
  144.         putln("MacBinary Protocol Disabled");
  145.  
  146.     (Ptr) mbfp = NewPtr( sizeof(MBFile));
  147.     if (mbfp==0L)
  148.         return(0L);
  149.     movmem( file, mbfp->name,64);
  150.     putln(mbfp->name);
  151.     c2pstr(mbfp->name);
  152.     mbfp->vrefnum=vrefnum;
  153.     mbfp->mode = mode;
  154.  
  155.     if ((err=FSOpen( mbfp->name, vrefnum, &mbfp->fd))) {
  156.         if ((err==-43) && (mode & MB_WRITE)) {
  157.             Create( mbfp->name, vrefnum, MyFavouriteTextEditor, 'TEXT');
  158.             if (FSOpen( mbfp->name, vrefnum, &mbfp->fd)) 
  159.                 return( 0L);
  160.             }
  161.         else 
  162.             return(0L);
  163.         }
  164.  
  165.     mbfp->binary=0;
  166.     mbfp->sector1=1;
  167.     mbfp->fork=0;
  168.     return( mbfp);
  169. }
  170.  
  171. int bwrite
  172.   (
  173.     MBFile *out,
  174.     char *buffer,
  175.     int size
  176.   )
  177. {
  178.     long len=size;
  179.     int error;
  180.  
  181.     error = 0;
  182.     
  183.     if (out->binary) {
  184.         if (out->bytes>0) {
  185.             if (out->bytes < len) len = out->bytes;
  186.             error= FSWrite( out->fd, &len, buffer);
  187.             out->bytes -= len;
  188.             buffer +=len;
  189.             size -= (int)len;
  190.             }
  191.         if (out->bytes<= 0) {
  192.             if (!out->fork) {
  193.                 out->fork=1;
  194.                 out->bytes=BLOCKS(out->rlen)*128;
  195.                 SetEOF( out->fd, (long) out->dlen);
  196.                 FSClose( out->fd);
  197.                 if (out->bytes) {
  198.                     OpenRF( out->name, out->vrefnum,&out->fd);
  199.                     if (size) {
  200.                         len = (long) size;
  201.                         error= FSWrite( out->fd, &len, buffer);
  202.                         }
  203.                     }
  204.                 else
  205.                     out->fd=0;
  206.                 }
  207.             else SetEOF( out->fd, (long) out->rlen);
  208.             }
  209.         }
  210.     else {
  211.         error = FSWrite( out->fd, &len, buffer);
  212.         }
  213.     return (error);
  214. }
  215.  
  216. void ProcessMBHead
  217.   (
  218.     MBFile *out,
  219.     MBHead *header
  220.   )
  221. {
  222.     int err;
  223.  
  224.     movmem( header, &out->header, sizeof(MBHead));
  225.     out->binary=1;
  226.     lmove( &header->dflen[0], &out->dlen);
  227.     lmove( &header->rflen[0], &out->rlen);
  228.     out->bytes= BLOCKS(out->dlen)*128;
  229.     out->fork=0;
  230.     out->sector1=0;
  231.  
  232.     FSClose(out->fd);
  233.     if (FSDelete( out->name, out->vrefnum))
  234.         putln("Error Deleting Old File ");
  235.         
  236.     movmem( &out->header.nlen, out->name,63);
  237.     
  238.     MBstat( &out->header.nlen, 1, (int)(BLOCKS(out->dlen)+BLOCKS(out->rlen)) );
  239.  
  240.     if (out->bytes) {
  241.         if ((err=FSOpen( out->name, out->vrefnum, &out->fd))) {
  242.             if (err=-43) {
  243.                 long cre,typ;
  244.  
  245. /*  this crashes Mac Pluses                
  246.                 typ = *(long *)out->header.type;
  247.                 cre = *(long *)out->header.creator;
  248. */
  249.                 lmove(out->header.type, &typ);
  250.                 lmove(out->header.creator, &cre);
  251.  
  252.                 Create( out->name, out->vrefnum, cre,typ);
  253.                 if (FSOpen( out->name, out->vrefnum, &out->fd)) 
  254.                     return;
  255.                 }
  256.             else {
  257.                 return;
  258.                 }
  259.             }
  260.         }
  261.     else {
  262.         if ((err=OpenRF( out->name, out->vrefnum, &out->fd))) {
  263.             if (err=-43) {
  264.                 long cre,typ;
  265.                 
  266. /*  this crashes Mac Pluses                
  267.                 typ = *(long *)out->header.type;
  268.                 cre = *(long *)out->header.creator;
  269. */
  270.                 lmove(out->header.type, &typ);
  271.                 lmove(out->header.creator, &cre);
  272.  
  273.                 Create( out->name, out->vrefnum, cre,typ);
  274.                 if (OpenRF( out->name, out->vrefnum, &out->fd)) 
  275.                     return;
  276.                 }
  277.             else {
  278.                 return;
  279.                 }
  280.             }
  281.         out->fork = 1;
  282.         out->bytes=BLOCKS(out->rlen)*128;
  283.         }
  284. }
  285.  
  286. int MBwrite
  287.   (
  288.     MBFile *out,
  289.     void *buffer,
  290.     int size
  291.   )
  292. {
  293.     int rsize;
  294.     
  295.     if (size < 1)
  296.         return(0);
  297.  
  298.     rsize=size;
  299.  
  300.     if (out->sector1 && (size >= sizeof(struct MBHead)) &&
  301.         isMacBinary(buffer) && (!(out->mode & MB_DISABLE))) {
  302.         putln("First sector of MacBinary file");
  303.         ProcessMBHead( out, (MBHead *) buffer);
  304.         (Ptr) buffer+=128;
  305.         if ((size-=128) <1)
  306.             return(rsize);
  307.         }
  308.  
  309.     if (bwrite( out,buffer,size))
  310.         return(-1);
  311.     else
  312.         return( rsize);
  313. }
  314.  
  315. void MBclose
  316.   (
  317.     MBFile *out
  318.   )
  319. {
  320.     FileParam finfo;
  321.     long fpos;
  322.  
  323.     putln("MBclose");
  324.     
  325.     if (!out->fd)
  326.         return;
  327.  
  328.     if (!(out->mode & MB_DISABLE) && (out->mode & MB_WRITE)) {
  329.         if (out->fork)
  330.             SetEOF( out->fd, (long) out->rlen);
  331.         else
  332.             SetEOF( out->fd, (long) out->dlen);
  333.  
  334.         FSClose( out->fd);
  335.         GetFileInfo( 0, out->name, &finfo);
  336.     
  337.         movmem( &out->header.type[0], &finfo.ioFlFndrInfo, sizeof(FInfo));
  338.         lmove( &out->header.cdate[0], &finfo.ioFlCrDat);
  339.         lmove( &out->header.mdate[0], &finfo.ioFlMdDat);
  340.         finfo.ioFlFndrInfo.fdFlags &= 0xfeff;
  341.         finfo.ioFlRLgLen=out->rlen;
  342.         finfo.ioFlLgLen =out->dlen;
  343.     
  344.         SetFileInfo( 0, out->name, &finfo);
  345.         }
  346.     else if (out->mode & MB_WRITE) {
  347.         GetFPos( out->fd, &fpos);
  348.         SetEOF(  out->fd,  fpos);
  349.         FSClose( out->fd);
  350.         }
  351.     else
  352.         FSClose( out->fd);
  353. }
  354.  
  355. int MBread
  356.   (
  357.     MBFile *in,
  358.     void *buffer,
  359.     int size
  360.   )
  361. {
  362.     char *p;
  363.     int rsize=size;
  364.  
  365.  
  366.     if (in->fork<0) {
  367.         return(-1);
  368.         }
  369.  
  370.     p=buffer;
  371.     if (in->sector1) {
  372.         FileParam finfo;
  373.  
  374.         setmem( &in->header, sizeof(MBHead), 0);
  375.         movmem(  in->name, &in->header.nlen, 64);
  376.         GetFileInfo( in->vrefnum, in->name, &finfo);
  377.         movmem( &finfo.ioFlFndrInfo, &in->header.type[0], sizeof(FInfo) );
  378.         in->header.protected = (in->header.zero2 & 0x40)?1:0;
  379.         in->header.zero2 = 0;
  380.         lmove( &finfo.ioFlLgLen, &in->header.dflen[0]);
  381.         lmove( &finfo.ioFlRLgLen,&in->header.rflen[0]);
  382.         lmove( &finfo.ioFlCrDat, &in->header.cdate[0]);
  383.         lmove( &finfo.ioFlMdDat, &in->header.mdate[0]);
  384.         in->dlen=finfo.ioFlLgLen;
  385.         in->rlen=finfo.ioFlRLgLen;
  386.         if (! (in->mode & MB_DISABLE) ) {
  387.             if (size<128) return(-1);
  388.  
  389.             movmem( &in->header, p, 128);
  390.             p +=128;
  391.             size -= 128;
  392.             in->bytes= BLOCKS(in->dlen)*128;
  393.             in->binary=1;
  394.             }
  395.         else {
  396.             in->bytes = in->dlen;
  397.             in->rlen=0;
  398.             in->binary=0;
  399.             }
  400.         in->sector1=0;
  401.         MBstat( &in->header.nlen, 1, (int) (BLOCKS(in->dlen)+BLOCKS(in->rlen)) );
  402.         }
  403.  
  404.     if ( size >0) {
  405.         long length = (long)size;
  406.         int err;
  407.  
  408.         err = FSRead( in->fd, &length, p);
  409.  
  410.         size -=(int)length;
  411.         in->bytes -=length;
  412.         p += length;
  413.  
  414.         if (err == -39 || (in->bytes<=0) ) {
  415.             FSClose( in->fd );
  416.             if (in->bytes<0L) in->bytes=0L;
  417.             size -= (int)in->bytes;
  418.             p    +=      in->bytes;                /* Make adjustments for necessary 128 byte term */
  419.             if (!in->fork ) {
  420.                 in->fork=1;
  421.                 in->bytes= BLOCKS(in->rlen)*128;
  422.                 if (in->bytes) {
  423.                     OpenRF( in->name, in->vrefnum, &in->fd);
  424. #ifdef READ
  425.                     length=(long)size;
  426.                     if (length >0L) {
  427.                         err = FSRead( in->fd, &length, p);
  428.                         size -= (int)length;
  429.                         in->bytes -=length;
  430.                         }
  431. #endif READ
  432.                     }
  433.                 else {
  434.                     in->fd=0;
  435.                     in->fork=-1;                    /* Time to close up shop */
  436.                     }
  437.                 }
  438.             else {
  439.                 in->fd=0;
  440.                 in->fork=-1;                    /* Time to close up shop */
  441.                 }
  442.             }
  443.         }
  444.     return( rsize-size); 
  445. }
  446.  
  447. init_mb_files() {            /* BYU */
  448.   ftp_mbfp->fd = 0;            /* BYU */
  449.   mbfp->fd = 0;                /* BYU */
  450. }                            /* BYU */
  451.                             /* BYU */
  452. close_mb_files() {            /* BYU */
  453.     if (ftp_mbfp->fd != 0) MBclose( ftp_mbfp );        /* BYU - close input file */
  454.     if (mbfp->fd != 0) MBclose( mbfp );                /* BYU - close input file */
  455. }                            /* BYU */
  456.